﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CubeCube.World.Generator
{
    /*
    * 获取generator进度的类
    */
    public class GeneratorProcess
    {
        private CubeWorldGenerator generator;
        private Chunk.Chunk chunk;

        private bool finished = false;

        private int totalCost;

        public GeneratorProcess(CubeWorldGenerator generator, Chunk.Chunk chunk)
        {
            this.finished = false;
            this.generator = generator;
            this.chunk = chunk;

            generator.Prepare();
            totalCost = generator.GetTotalCost();
        }

        public bool Generate()
        {
            if (finished == false)
            {
                if(chunk != null)
                {
                    if (generator.Generate(chunk) == true)
                        finished = true;
                }
                else
                {
                    if (generator.Generate() == true)
                        finished = true;
                }
            }

            return finished;
        }

        public int GetProgress()
        {
            if (totalCost != 0)
                return generator.GetCurrentCost() * 100 / totalCost;
            else
                return 100;
        }

        public bool IsFinished()
        {
            return finished;
        }

        public override string ToString()
        {
            return generator.ToString();
        }
    }
}
